#include <QSpinBox>
#include "ItemInt.h"


int ItemInt::get()
{
    return m_data;
}

void ItemInt::set(const int &value)
{
    m_data = value;
    emit itemEdited();
}

QWidget *ItemInt::createWidget(WidgetStyle s)
{
    auto w = new QSpinBox;
    w->setRange(0, 9999);
    connect(w, QOverload<int>::of(&QSpinBox::valueChanged), this, &ItemInt::onWidgetEditedWrapper);
    return w;
}

void ItemInt::update(QWidget *widget, UpdateOption option)
{
    QSpinBox *spinBox = qobject_cast<QSpinBox*>(widget);
    if (spinBox != nullptr)
    {
        if (option == UpdateOption::WidgetContent)
        {
            spinBox->setValue(m_data);
        }
        else if (option == UpdateOption::Item)
        {
            this->m_data = spinBox->value();
            qInfo() << "update spinBox: " << this->m_data;
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemInt::writeToData(QDataStream &stream) const
{
    stream << qint32(m_data);
}

void ItemInt::readFromData(QDataStream &stream)
{
    qint32 temp;
    stream >> temp;
    m_data = temp;
    emit itemEdited();
}
